package com.jyy.prefabricated.controller;


import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.AnswerDto;
import com.jyy.prefabricated.entity.Student;
import com.jyy.prefabricated.service.StudentErrorQuestionService;
import com.jyy.prefabricated.utils.UserUtils;
import com.jyy.prefabricated.vo.ExamResultVo;
import com.jyy.prefabricated.vo.StudentExercisePaperVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 学员练习题目
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class StudentErrorQuestionController {

    @Autowired
    private StudentErrorQuestionService studentErrorQuestionService;

    @Autowired
    private UserUtils userUtils;

    /**
     * 错题重做答题
     * @param dto
     * @return
     */
    @PutMapping("/student-error-question")
    public AjaxResult update(@Valid @RequestBody AnswerDto dto){
        log.info("Rest request to update studentExamQuestion:{}",dto);
        StudentExercisePaperVo result = studentErrorQuestionService.answer(dto);
        return AjaxResult.success(result);
    }

    /**
     * 获取错题
     * @param examPaperId 练习id
     * @return
     */
    @GetMapping("/student-error-question/{examPaperId}")
    public AjaxResult selectByExamPaperId(@PathVariable Integer examPaperId){
        Student student = userUtils.getCurrentStudent();
        StudentExercisePaperVo result = studentErrorQuestionService.fetchQuestionsByExamPaperIdAndStudentId(examPaperId,student.getId());
        return AjaxResult.success(result);
    }

    /**
     * 错题交卷,返回数据及处理错题
     * @param examPaperId
     * @return
     */
    @PutMapping("/student-error-question-result/{examPaperId}")
    public AjaxResult finish(@PathVariable Integer examPaperId){

        ExamResultVo result = studentErrorQuestionService.finishErrorExercise(examPaperId);
        return AjaxResult.success(result);
    }


}
